<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><%= title %></title>
    <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div id="container" class="container">
    <%if(locals.success){%>
    <div id="alt_success" class="alert alert-success">
        <%- success%>
    </div>
    <%}%>
    <%if(locals.error){%>
    <div id="alt_warning" class="alert alert-warning">
        <%=error%>
    </div>
    <%}%>
        <!--返回上一页-->
        <ul class="pagination pagination-sm" >
            <li><a href="/index" class="glyphicon glyphicon-home" title="返回主页"></a></li>
        </ul>
    <form class="form-signin" role="form" method="post"  enctype="multipart/form-data">
        <h2 class="form-signin-heading" >upload file</h2>
        <input type="file" name="fulAvatar" id="fulAvatar" class="form-control">
        <br>
        <button id="butSub" class="btn btn-lg btn-primary">upload</button>

    </form>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript">
    String.prototype.format = function (args) {
        var result = this;
        if (arguments.length > 0) {
            if (arguments.length == 1 && typeof (args) == "object") {
                for (var key in args) {
                    if (args[key] != undefined) {
                        var reg = new RegExp("({" + key + "})", "g");
                        result = result.replace(reg, args[key]);
                    }
                }
            }
            else {
                for (var i = 0; i < arguments.length; i++) {
                    if (arguments[i] != undefined) {
                        var reg = new RegExp("({)" + i + "(})", "g");
                        result = result.replace(reg, arguments[i]);
                    }
                }
            }
        }
        return result;
    }

    $(function(){
        $('#btnSub').on('click',function(){
            var fulAvatarVal = $('#fulAvatar').val(),
                errorTip = '<div id="errorTip" class="alert alert-warning">{0}</div> ';

            $("#errorTip,#alt_warning").remove();

            if(fulAvatarVal.length == 0)
            {
                $("#container").prepend(errorTip.format('请选择要上传的文件'));
                return false;
            }

            var extName = fulAvatarVal.substring(fulAvatarVal.lastIndexOf('.'),fulAvatarVal.length).toLowerCase();

            if(extName != '.png' && extName != '.jpg'){
                $("#container").prepend(errorTip.format('只支持png和jpg格式图片'));
                return false;
            }
            return true;
        })
    });

</script>
</body>
</html>

